home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / EXAMPLES / BITFONT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  2.6 KB  |  131 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. #include <string.h>
  9. #include <GL/glut.h>
  10.  
  11. void *font = GLUT_BITMAP_TIMES_ROMAN_24;
  12. void *fonts[] =
  13. {
  14.   GLUT_BITMAP_9_BY_15,
  15.   GLUT_BITMAP_TIMES_ROMAN_10,
  16.   GLUT_BITMAP_TIMES_ROMAN_24
  17. };
  18. char defaultMessage[] = "GLUT means OpenGL.";
  19. char *message = defaultMessage;
  20.  
  21. void
  22. selectFont(int newfont)
  23. {
  24.   font = fonts[newfont];
  25.   glutPostRedisplay();
  26. }
  27.  
  28. void
  29. selectMessage(int msg)
  30. {
  31.   switch (msg) {
  32.   case 1:
  33.     message = "abcdefghijklmnop";
  34.     break;
  35.   case 2:
  36.     message = "ABCDEFGHIJKLMNOP";
  37.     break;
  38.   }
  39. }
  40.  
  41. void
  42. selectColor(int color)
  43. {
  44.   switch (color) {
  45.   case 1:
  46.     glColor3f(0.0, 1.0, 0.0);
  47.     break;
  48.   case 2:
  49.     glColor3f(1.0, 0.0, 0.0);
  50.     break;
  51.   case 3:
  52.     glColor3f(1.0, 1.0, 1.0);
  53.     break;
  54.   }
  55.   glutPostRedisplay();
  56. }
  57.  
  58. void
  59. tick(void)
  60. {
  61.   glutPostRedisplay();
  62. }
  63.  
  64. void
  65. output(int x, int y, char *string)
  66. {
  67.   int len, i;
  68.  
  69.   glRasterPos2f(x, y);
  70.   len = (int) strlen(string);
  71.   for (i = 0; i < len; i++) {
  72.     glutBitmapCharacter(font, string[i]);
  73.   }
  74. }
  75.  
  76. void
  77. display(void)
  78. {
  79.   glClear(GL_COLOR_BUFFER_BIT);
  80.   output(0, 24, "This is written in a GLUT bitmap font.");
  81.   output(100, 100, message);
  82.   output(50, 145, "(positioned in pixels with upper-left origin)");
  83.   glutSwapBuffers();
  84. }
  85.  
  86. void
  87. reshape(int w, int h)
  88. {
  89.   glViewport(0, 0, w, h);
  90.   glMatrixMode(GL_PROJECTION);
  91.   glLoadIdentity();
  92.   gluOrtho2D(0, w, h, 0);
  93.   glMatrixMode(GL_MODELVIEW);
  94. }
  95.  
  96. int
  97. main(int argc, char **argv)
  98. {
  99.   int i, msg_submenu, color_submenu;
  100.  
  101.   glutInit(&argc, argv);
  102.   for (i = 1; i < argc; i++) {
  103.     if (!strcmp(argv[i], "-mono")) {
  104.       font = GLUT_BITMAP_9_BY_15;
  105.     }
  106.   }
  107.   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  108.   glutInitWindowSize(500, 150);
  109.   glutCreateWindow("GLUT bitmap font example");
  110.   glClearColor(0.0, 0.0, 0.0, 1.0);
  111.   glutDisplayFunc(display);
  112.   glutReshapeFunc(reshape);
  113.   glutIdleFunc(tick);
  114.   msg_submenu = glutCreateMenu(selectMessage);
  115.   glutAddMenuEntry("abc", 1);
  116.   glutAddMenuEntry("ABC", 2);
  117.   color_submenu = glutCreateMenu(selectColor);
  118.   glutAddMenuEntry("Green", 1);
  119.   glutAddMenuEntry("Red", 2);
  120.   glutAddMenuEntry("White", 3);
  121.   glutCreateMenu(selectFont);
  122.   glutAddMenuEntry("9 by 15", 0);
  123.   glutAddMenuEntry("Times Roman 10", 1);
  124.   glutAddMenuEntry("Times Roman 24", 2);
  125.   glutAddSubMenu("Messages", msg_submenu);
  126.   glutAddSubMenu("Color", color_submenu);
  127.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  128.   glutMainLoop();
  129.   return 0;             /* ANSI C requires main to return int. */
  130. }
  131.